package com.shfl.servermanage.logic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.logic.bean.SysExecutableFile;
import com.shfl.servermanage.logic.constants.CommonConstants;
import com.shfl.servermanage.logic.mapper.SysExecutableFileMapper;
import com.shfl.servermanage.logic.model.info.AddSingleAttachmentInfo;
import com.shfl.servermanage.logic.model.request.AddExecutableFileRequest;
import com.shfl.servermanage.logic.model.request.RemoveByIdRequest;
import com.shfl.servermanage.logic.model.response.SysExecutableFilePageResponse;
import com.shfl.servermanage.logic.service.SysAttachmentService;
import com.shfl.servermanage.logic.service.SysExecutableFileService;
import com.shfl.servermanage.pagehelper.PageHelperUtils;
import com.shfl.servermanage.pagehelper.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SysExecutableFileServiceImpl implements SysExecutableFileService {

    @Autowired
    private SysExecutableFileMapper sysExecutableFileMapper;

    @Autowired
    private SysAttachmentService sysAttachmentService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddExecutableFileRequest addExecutableFileRequest) {
        SysExecutableFile sysExecutableFile = new SysExecutableFile();
        BeanUtil.copyProperties(addExecutableFileRequest, sysExecutableFile, false);
        sysExecutableFileMapper.insertSelective(sysExecutableFile);
        sysAttachmentService.addSingle(new AddSingleAttachmentInfo(
                addExecutableFileRequest.getFileId(),
                sysExecutableFile.getId(),
                CommonConstants.AttachType.EXECUTABLE_FILES.getValue()
        ));

    }

    @Override
    public SysExecutableFile selectByPrimaryKey(Long id) {
        return sysExecutableFileMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<SysExecutableFilePageResponse> findPage(Pageable pageable, Map<String, Object> objectMap) {
        PageHelperUtils.startPageWithCount(pageable);
        return new PageInfo<>(sysExecutableFileMapper.findPage(objectMap));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(RemoveByIdRequest removeByIdRequest) {
        sysExecutableFileMapper.logicalDeleteByPrimaryKey(removeByIdRequest.getId());
    }

    @Override
    public List<SysExecutableFilePageResponse> findList(Map<String, Object> objectMap) {
        return sysExecutableFileMapper.findPage(objectMap);
    }
}
